<?php
namespace app\controllers;

use fastphp\base\Controller;
use app\models\Error;
use app\models\Problem;
use app\models\Submit;
use app\models\User;

class SubmitController extends Controller
{
    public function index()
    {
        $submit = (new Submit())->init();
        if (($id = @$_POST['problem']) !== null && User::isLogin()) {
            if (($language = @$_POST['language']) !== null && ($code = @$_POST['code']) !== null) {
                $submit->submit($id, $language, $code);
                exit();
            }
        } elseif (($id = @$_GET['problem']) === null || !($problem = (new Problem())->get($id))) {
            Error::error(404);
        }
        $this->assign('id', $problem['id']);
        $this->assign('languages', $submit->languages);
        $this->assign('title', '提交代码');
        $this->render();
    }
}